// Fill out your copyright notice in the Description page of Project Settings.


#include "Layers/MotionSubtitlesSpriteLayer.h"

#include "Components/SceneCaptureComponent2D.h"

UMotionSubtitlesSpriteLayer::UMotionSubtitlesSpriteLayer()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
	SpritePlane = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Sprite Plane"));
	SpritePlane->SetupAttachment(this);
	SpritePlane->SetRelativeLocation(FVector(0, 0, 0));
	SpritePlane->SetRelativeRotation(FRotator(0, 90, 90));
	SpritePlane->SetRelativeScale3D(FVector(19.20, 10.80, 1));
	SpritePlane->bVisibleInSceneCaptureOnly = true;

	struct FConstructorStatics
	{
		ConstructorHelpers::FObjectFinder<UStaticMesh> Plane;

		FConstructorStatics(): Plane(TEXT("/MotionSubtitles/StaticMeshes/SM_Plane"))
		{
		}
	};

	static FConstructorStatics ConstructorStatics;
	if (ConstructorStatics.Plane.Object != nullptr)
	{
		UE_LOG(LogTemp, Log, TEXT("PlaneStaticMesh Loaded."));
		SpritePlane->SetStaticMesh(ConstructorStatics.Plane.Object);
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("PlaneStaticMesh Load failed."));
	}

	SceneCaptureComponent2D->ShowOnlyComponent(SpritePlane);

	LayerMask = ELayerMask::Layer0;
}


void UMotionSubtitlesSpriteLayer::Init(const FUint32Vector2& ViewSize)
{
	Super::Init(ViewSize);

	if (MaterialInst == nullptr)
	{
		const auto Material = LoadObject<UMaterial>(nullptr, TEXT("/Script/Engine.Material'/MotionSubtitles/Materials/M_RenderSprite.M_RenderSprite'"));
		MaterialInst = SpritePlane->CreateAndSetMaterialInstanceDynamicFromMaterial(0, Material);
	}
	MaterialInst->SetTextureParameterValue(TEXT("Sprite"), Sprite);

	SpritePlane->SetRelativeScale3D(FVector(ViewSize.X / 100.0f, ViewSize.Y / 100.0f, 1));
}

void UMotionSubtitlesSpriteLayer::Show(const FSubtitleParams& Params)
{
	Super::Show(Params);
}

void UMotionSubtitlesSpriteLayer::Hide()
{
	Super::Hide();
}

void UMotionSubtitlesSpriteLayer::Exit()
{
	Super::Exit();
}

void UMotionSubtitlesSpriteLayer::DoScale(const EAnimTarget AnimTarget, const int32 ElementId, const float Value)
{
	// Sprite层的WholeLayer、ContentElement模式都只对SpritePlane进行操作
	if (AnimTarget == EAnimTarget::WholeLayer)
	{
		SpritePlane->SetRelativeScale3D(OriginScale * Value);
	}
	else
	{
		SpritePlane->SetRelativeScale3D(OriginScale * Value);
	}
}

void UMotionSubtitlesSpriteLayer::DoTranslate(const EAnimTarget AnimTarget, const int32 ElementId,const FVector Value)
{
	// Sprite层的WholeLayer、ContentElement模式都只对SpritePlane进行操作
	if (AnimTarget == EAnimTarget::WholeLayer)
	{
		SpritePlane->SetRelativeLocation(OriginLocation + Value);
	}
	else
	{
		SpritePlane->SetRelativeLocation(OriginLocation + Value);
	}
}

void UMotionSubtitlesSpriteLayer::DoFade(const EAnimTarget AnimTarget, const int32 ElementId,const float Value)
{
	// Sprite层的WholeLayer、ContentElement模式都只对SpritePlane进行操作
	UMaterialInterface* Material = SpritePlane->GetMaterial(0);
	if (AnimTarget == EAnimTarget::WholeLayer)
	{
		if(UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Material))
		{
			DynamicMaterial->SetScalarParameterValue(TEXT("AlphaFactor"), Value);
		}
	}
	else
	{
		if(UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Material))
		{
			DynamicMaterial->SetScalarParameterValue(TEXT("AlphaFactor"), Value);
		}
	}
}

void UMotionSubtitlesSpriteLayer::DoDissolve(const EAnimTarget AnimTarget, const int32 ElementId, const float Value)
{
	// Sprite层的WholeLayer、ContentElement模式都只对SpritePlane进行操作
	UMaterialInterface* Material = SpritePlane->GetMaterial(0);
	if (AnimTarget == EAnimTarget::WholeLayer)
	{
		if(UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Material))
		{
			DynamicMaterial->SetScalarParameterValue(TEXT("Dissolve"), Value);
		}
	}
	else
	{
		if(UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(Material))
		{
			DynamicMaterial->SetScalarParameterValue(TEXT("Dissolve"), Value);
		}
	}
}

void UMotionSubtitlesSpriteLayer::CaptureStatus()
{
	OriginScale = SpritePlane->GetRelativeScale3D();
	OriginLocation = SpritePlane->GetRelativeLocation();
}

int32 UMotionSubtitlesSpriteLayer::ElementCount()
{
	return 1;
}
